UPDATE Alarm SET SetPoint = 253000 WHERE SetPoint = 255000

INSERT INTO Measurement (DeviceID, HistorianID, SignalTypeID, PointTag, SignalReference, Description, Enabled)
SELECT 
	Device.ID,
	(SELECT ID FROM Historian WHERE Acronym LIKE 'STAT'),
	(SELECT ID FROM SignalType WHERE Acronym LIKE 'ALRM'),
	CONCAT('AL-LOW-VOLT-STATUS:',AM.PointTag),
	CONCAT('AL-LOW-VOLT-STATUS:',AM.PointTag),
	CONCAT('Alarm Voltage < 10kV for ', Device.Name),
	(SELECT 1) FROM 
	Device Inner Join ActiveMeasurement AM ON AM.DeviceID = Device.ID AND AM.SignalType LIKE 'VPHM' AND AM.Phase LIKE '+' AND (SELECT TOP 1 BaseKV FROM PhasorDetail WHERE AM.PhasorID = PhasorDetail.ID) = 220 WHERE IsConcentrator <> 1
GO

INSERT INTO Alarm (NodeID, TagName, Tolerance, AssociatedMeasurementID, Delay, Description, Hysteresis, SignalID, SetPoint, Severity, Operation) 
SELECT 
	(SELECT TOP 1 ID FROM Node), 
	CONCAT('AL-LOW-VOLT-STATUS:',AM.PointTag),
	NULL,
	AM.SignalID,
	1E-5, 
	CONCAT('Voltage < 10kV for ', Device.Name),
	1E-5,
	AMA.SignalID,
	10000,
	50,
	21
	FROM 
		Device 
		INNER JOIN ActiveMeasurement AM ON AM.DeviceID = Device.ID AND AM.SignalType LIKE 'VHPM' AND AM.Phase LIKE '+' AND (SELECT TOP 1 BaseKV FROM PhasorDetail WHERE AM.PhasorID = PhasorDetail.ID) = 220 
		LEFT JOIN Measurement AMA ON AMA.DeviceID = Device.ID AND AMA.PointTag LIKE 'AL-LOW-VOLT-ALARM:%' WHERE IsConcentrator <> 1
GO
